﻿@inherits LayoutComponentBase

<Layout>
    <Header Class="header">
        <Row Align="center">
            <Col Span="4"><h3>身份认证中心</h3></Col>
            <Col Span="18"></Col>
            <Col Span="2">
            <div>
                <Menu Mode=MenuMode.Horizontal Theme="MenuTheme.Dark">
                    @{
                        RenderFragment temp = @<span style="color:#fff">
                            <Avatar Size="36" Style="background-color: #87d068;margin:0px;" Icon="user" />
                            妄揽明月
                        </span>;
                    }
                    <SubMenu Title=temp>
                        <MenuItem Key="usermeun1">个人信息</MenuItem>
                        <MenuItem Key="usermeun2">修改密码</MenuItem>
                        <MenuItem Key="usermeun3" @onclick="Logout">登出</MenuItem>
                    </SubMenu>
                </Menu>

            </div>
            </Col>
        </Row>

    </Header>
    <Layout Style="min-height: 100vh;">
        <Sider Width="200" Style="background-color: #001529" Class="site-layout-background" Collapsible="true" Theme="SiderTheme.Dark">
            <Menu Mode="MenuMode.Inline"
                  DefaultSelectedKeys=@(new[] {_menuInitModels.FirstOrDefault().Key})
                  DefaultOpenKeys=@(new[] {_menuInitModels.FirstOrDefault().Key})
                  Style="height: 100%; border-right: 0;"
                  Theme="MenuTheme.Dark"
                  OnMenuItemClicked="MeunClick">
                @meunRender
                @*@foreach (var menu in _menuInitModels)
                        {
                            if (menu.Subs == null)
                            {
                                if (!meunPathDic.ContainsKey(menu.Key))
                                {
                                    meunPathDic.Add(menu.Key, menu.Title);
                                }

                                <MenuItem Key="@menu.Key" Style="margin:0px" RouterLink="@menu.Router" RouterMatch="NavLinkMatch.All">
                                    <Icon Type="@menu.Type" Theme="outline"></Icon>
                                    <span>@menu.Title</span>
                                </MenuItem>
                            }
                            else
                            {
                                RenderFragment temp = @<span>
                            <Icon Type="@menu.Type" Theme="outline"></Icon>
                            @menu.Title
                        </span>;
                        <SubMenu Key="@menu.Key" Title=temp>
                            @foreach (var subMenu in menu.Subs)
                            {
                                if (!meunPathDic.ContainsKey(subMenu.Key))
                                {
                                    meunPathDic.Add(subMenu.Key, $"{menu.Title},{subMenu.Title}");
                                }
                                <MenuItem Key="@subMenu.Key" Style="margin:0px" RouterLink="@subMenu.Router" RouterMatch="NavLinkMatch.All">
                                    <Icon Type="@subMenu.Type" Theme="outline"></Icon>
                                    <span>@subMenu.Title</span>
                                </MenuItem>
                            }
                        </SubMenu>
                    }*@
            </Menu>
        </Sider>
        <Layout Style=" padding: 0 24px 24px;">
            <Breadcrumb Style="margin: 16px 0;">
                @foreach (var bread in _breadcrumbItem)
                {
                    <BreadcrumbItem>@bread</BreadcrumbItem>
                }
            </Breadcrumb>
            <Content Class="site-layout-background" Style=" padding: 24px; margin: 0; min-height: 280px;">
                <AuthorizeView>
                    <Authorized>
                        @Body
                    </Authorized>
                    <NotAuthorized>
                        <ToLogin></ToLogin>
                    </NotAuthorized>
                </AuthorizeView>
            </Content>
            <Footer Style="text-align: center;">Ant Design ©2020 Created by 妄揽明月</Footer>
        </Layout>
    </Layout>
</Layout>
@code{
    RenderFragment meunRender;
    List<string> _breadcrumbItem = new List<string>();
    Dictionary<string, string> meunPathDic = new Dictionary<string, string>();
    List<MenuInitModel> _menuInitModels = new List<MenuInitModel>
{
        new MenuInitModel
        {
            Title="主页",
            Router="/",
        },
        new MenuInitModel
        {
            Title="用户管理",
            Subs = new List<MenuInitModel>
            {
                new MenuInitModel
                {
                    Title="用户列表",
                    Router="/User",
                },
                new MenuInitModel
                {
                    Title="声明列表",
                    Router="/User/Claims",
                }
            }
        }
    };
    protected override Task OnInitializedAsync()
    {
        meunRender = CreateMenu(_menuInitModels);
        return base.OnInitializedAsync();
    }
    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            _breadcrumbItem = meunPathDic.FirstOrDefault().Value.Split(",").ToList();

        }
        return base.OnAfterRenderAsync(firstRender);
    }
    RenderFragment CreateMenu(List<MenuInitModel> meunList, string parentTitle = "")
    {
        RenderFragment sub = default;
        foreach (var menu in meunList)
        {
            if (menu.Subs == null)
            {
                if (!meunPathDic.ContainsKey(menu.Key))
                {
                    var title = string.IsNullOrEmpty(parentTitle) ? menu.Title : $"{parentTitle},{menu.Title}";
                    Console.WriteLine(title);
                    meunPathDic.Add(menu.Key, $"{title}");
                }
                sub +=@<MenuItem Key="@menu.Key" Style="margin:0px" RouterLink="@menu.Router" RouterMatch="NavLinkMatch.All">
        <Icon Type="@menu.Type" Theme="outline"></Icon>
        <span>@menu.Title</span>
    </MenuItem>;
 }
 else
 {
     RenderFragment temp = @<span>
    <Icon Type="@menu.Type" Theme="outline"></Icon>
    @menu.Title
</span>;
sub += @<SubMenu Key="@menu.Key" Title=temp>
    @{
        var title = string.IsNullOrEmpty(parentTitle) ? menu.Title : $"{parentTitle},{menu.Title}";
        Console.WriteLine(title);
    }
    @CreateMenu(menu.Subs, title)
</SubMenu>;
}
}
return sub;
}
void MeunClick(MenuItem e)
{
Console.WriteLine(e.Key);
_breadcrumbItem = meunPathDic[e.Key].Split(",").ToList();
}
async Task Logout()
{
await JSRuntime.InvokeVoidAsync("logout");
}
}
<style>


    .site-layout-background {
        background: #fff;
    }

    .header h3 {
        color: #fff
    }

    .header {
        color: #fff
    }
</style>
